﻿using PoliticalClassifier.Support;
using System;
using System.Collections;
using System.Collections.Generic;

namespace PoliticalClassifier.Framework
{
    /// <summary>
    /// CandidateManager class responsible to manage candidates in a single application instance.
    /// </summary>
	public class CandidateManager : ICandidateManager
	{
		#region Contruction
        /// <summary>
        /// Construct a candidate manager object.
        /// </summary>
		public CandidateManager()
		{
		}
		#endregion

		#region Public Method
		/// <summary>
		/// Add a new candidate in a manager.
		/// </summary>
		/// <param name="candidate">ICandidate object</param>
		/// <returns>zero based index location of the added candidate.</returns>
		public int AddCandidate(ICandidate candidate)
		{
			for (int i = 0; i < CandidateList.Count; i++)
			{
				if (CandidateList[i].Name.Equals(candidate.Name, StringComparison.OrdinalIgnoreCase))
				{
					CandidateList.RemoveAt(i);
					break;
				}
			}

			CandidateList.Add(candidate);

			return Count - 1;
		}

		/// <summary>
		/// Remove a candidate from the manager
		/// </summary>
		/// <param name="index">zero based index location of the candiate.</param>
		public void RemoveCandidate(int index)
		{
			if (index >= Count)
				return;

			CandidateList.RemoveAt(index);
		}

		/// <summary>
		/// Gets a strongly-typed enumerator over the candidates.
		/// </summary>
		/// <returns>The strongly-typed enumerator.</returns>
		public IEnumerator<ICandidate> GetEnumerator()
		{
			return CandidateList.GetEnumerator();
		}

		/// <summary>
		/// Gets a weakly-typed enumerator over the candidates.
		/// </summary>
		/// <returns>A weakly-typed enumerator.</returns>
		IEnumerator IEnumerable.GetEnumerator()
		{
			return ((IEnumerable)CandidateList).GetEnumerator();
		}
		#endregion

		#region Public Properties
		/// <summary>
		/// Returns the number of candidates in a manager
		/// </summary>
		public int Count
		{
			get { return CandidateList.Count; }
		}

		/// <summary>
		/// Gets the candidate at the given index.
		/// </summary>
		/// <param name="index">The index to get the candidate at.</param>
		/// <returns>The candidate.</returns>
		public ICandidate this[int index]
		{
			get { return CandidateList[index]; }
		}
		#endregion 

		#region Private Properties
		/// <summary>
		/// returns the candidate list.
		/// </summary>
		private IList<ICandidate> CandidateList
		{
			get { return m_listCandidate; }
		}
		#endregion

		#region Private Fields
		IList<ICandidate> m_listCandidate = new List<ICandidate>();
		#endregion
	}
}
